package com.tpc.eb.utils;

import com.alibaba.druid.pool.DruidDataSource;
import org.ini4j.Profile;
import org.ini4j.Wini;

import java.io.File;
import java.sql.*;

public class JDBCUtil {

    private static DruidDataSource dataSource = null;

    static {
        String filePath = null;
        try {
            filePath = FileUtil.getFilePath();
            Wini wini = new Wini(new File(filePath));
            Profile.Section section = wini.get("DBConfig");
            String dbType = section.get("dbType");
            String server = section.get("server");
            String userName = section.get("userName");
            String passWord = section.get("passWord");
            String dataBase = section.get("dataBase");
            String port = section.get("port");
            String driver = "";
            String url = "" ;
            if(dbType.equals("MSSQL")){
                driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                url = "jdbc:sqlserver://" + server + ":" + port + ";DatabaseName=" + dataBase;
            }else if(dbType.equals("ORACLE")){
                driver = "oracle.jdbc.OracleDriver";
                url = "jdbc:oracle:thin:@" + server + ":" + port + ":" + dataBase;
            }else if(dbType.equals("MYSQL")){
                driver = "com.mysql.jdbc.Driver";
                url = "jdbc:mysql://"+ server + ":" + port + "/" + dataBase;
            }
            dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(userName);
            dataSource.setPassword(passWord);
            dataSource.setMinIdle(5);
            dataSource.setMaxActive(200);
            dataSource.setRemoveAbandoned(true);
            dataSource.setRemoveAbandonedTimeout(60);
            dataSource.setMaxWait(10000);
            dataSource.setInitialSize(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        Connection conn = dataSource.getConnection();
        return conn;
    }

    public static void close(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn=null;
        }
    }

    public static void close(Statement stmt , Connection conn){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt=null;
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn=null;
        }

    }

    public static void close(ResultSet rs , Statement stmt , Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs=null;
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt=null;
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn=null;
        }

    }





}
